<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>更新页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/component/jsoneditor/css/jsoneditor.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
        
    </head>
    <body>

        <form class="layui-form">

            <div class="mainBox">
                <div class="main-container mr-5">
                    <div class="layui-form-item">
                        <label class="layui-form-label required">章节名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" placeholder="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">章节序号</label>
                        <div class="layui-input-block">
                            <input type="number" name="number" placeholder="" class="layui-input">
                        </div>
                    </div>
                    <div style="display: flex;flex-direction: row;align-items: center">
                        <div class="layui-form-item" style="width: 50%">
                            <label class="layui-form-label required">原文内容</label>
                            <div class="layui-input-block">
                                <tip style="color: red"> 当前字数：<span class="original_char_nums">0</span></tip>
                                <textarea name="original_content" class="layui-textarea" rows="15" cols="27" onblur="comput_original_nums()"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item"  style="width: 50%;display: flex;flex-direction: row;">
                            <div style="display: flex;flex-direction: column;">
                                <label class="layui-form-label">原文分析AI指令：</label>
                                <button type="button" class="layui-btn layui-btn-radius edit_original_content_ai_command" style="margin-left: 9px">
                                    更新指令
                                </button>
                            </div>
                            <div class="layui-input-block" style="margin-left: 10px;">
                                <tip style="color: red;font-size: 12px;">@content 作为内容替换</tip>
                                <textarea name="original_content_ai_command_overseas" class="layui-textarea" rows="15" cols="100"></textarea>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"></label>
                        <button type="button" class="pear-btn pear-btn-primary pear-btn-md original_content_analyze">
                            原文分析
                        </button>
                    </div>
                    <div style="display: flex;flex-direction: row;align-items: center">
                        <div class="layui-form-item" style="width: 90%">
                            <label class="layui-form-label required">分析内容</label>
                            <div class="layui-input-block">
                                <tip style="color: red"> 当前字数：<span class="analyze_char_nums">0</span></tip>
                                <textarea name="content" class="layui-textarea" rows="15" cols="27" onblur="comput_analyze_nums()"></textarea>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                        提交
                    </button>
                    <!--<button type="reset" class="pear-btn pear-btn-md">-->
                        <!--重置-->
                    <!--</button>-->
                </div>
            </div>
            <div style="display: none;">
                <textarea name="original_content_ai_command_config" class="layui-textarea" rows="15"  cols="23"><?=htmlspecialchars($original_content_ai_command_overseas)?></textarea>
            </div>
        </form>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/component/jsoneditor/jsoneditor.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/functions.js"></script>
        <script src="/app/admin/admin/js/md5.js"></script>
        
        <script>
            // 相关接口
            const PRIMARY_KEY = "id";
            const PRIMARY_KEY_VALUE = layui.url().search[PRIMARY_KEY];
            const LANG_VALUE = layui.url().search['lang'];
            const SELECT_API = "/app/admin/novel/chapter/select" + location.search;
            const UPDATE_API = "/app/admin/novel/chapter/update";
            const AI_REGENERATE_API = "/app/admin/novel/chapter/airegenerate";
            const GPT_SELECT_API = "/app/admin/gptversion/index/select?limit=100";

            // 获取数据库记录
            layui.use(["form", "util", "popup"], function () {
                $ = layui.$;
                let loading = layer.load();
                $.ajax({
                    url: SELECT_API,
                    dataType: "json",
                    async:true,
                    headers: get_header({"id":PRIMARY_KEY_VALUE,"lang":LANG_VALUE}),
                    success: function (res) {
                        layer.close(loading);
                        // 给表单初始化数据
                        layui.each(res.data[0], function (key, value) {
                            let obj = $('*[name="'+key+'"]');
                            if (key === "password") {
                                obj.attr("placeholder", "不更新密码请留空");
                                return;
                            }
                            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                            if (obj[0].nodeName.toLowerCase() === "textarea") {
                                obj.val(value);
                            } else {
                                obj.attr("value", value);
                                obj[0].value = value;
                            }
                        });


                        // let param = {"status":1};
                        // layui.$.ajax({
                        //     url: GPT_SELECT_API,
                        //     type: "GET",
                        //     dateType: "json",
                        //     data: param,
                        //     async:true,
                        //     headers: get_header(param),
                        //     success: function (res2) {
                        //
                        //         if (res2.code) {
                        //             return layui.popup.failure(res2.msg);
                        //         }
                        //         let html ='';
                        //         let checked ='';
                        //         layui.$.each(res2.data,function (i,v) {
                        //             checked ='';
                        //             if(res.data[0]['gptversion'] && res.data[0]['gptversion']==v['name']){
                        //                 checked ='checked';
                        //             }else if(res.data[0]['gptversion']==false && v['name']=='gpt-4o'){
                        //                 checked ='checked';
                        //             }
                        //             html += '<input type="radio" name="gptversion" value="'+v['name']+'" title="'+v['name']+'" '+checked+'>';
                        //         })
                        //         $('.gptversion').html(html);
                        //
                        //         layui.form.render();
                        //     }
                        // });

                        comput_original_nums()
                        comput_analyze_nums()

                        // ajax返回失败
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }
                        
                    }
                });
            });

            //提交事件
            layui.use(["form", "popup","loading"], function () {
                layui.form.on("submit(save)", function (data) {
                    data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    data.field['lang'] = LANG_VALUE;
                    data.field['content'] = data.field['analyze_content'];
                    let loading = layer.load();
                    layui.$.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        async:true,
                        headers: get_header(data.field),
                        success: function (res) {
                            layer.close(loading);
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

            layui.use(["form", "popup","loading"], function () {
                layui.$('.original_content_analyze').click(function () {
                    let param = {};
                    param['original_content'] = layui.$('textarea[name="original_content"]').val();
                    param['original_content_ai_command_overseas'] = layui.$('textarea[name="original_content_ai_command_overseas"]').val();
                    param['type'] = 'original_content_analyze_overseas';
                    param['lang'] = LANG_VALUE;

                    let loadIndex = layui.loading.Load(1, "生成中...");
                    layui.$.ajax({
                        url: AI_REGENERATE_API,
                        type: "POST",
                        dateType: "json",
                        data: param,
                        async:true,
                        headers: get_header(param),
                        success: function (res) {
                            layui.loading.loadRemove(1);
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                layui.$('textarea[name="content"]').val(res.data.content);
                                layui.$('.analyze_char_nums').text(res.data.nums);
                                comput_analyze_nums()
                            });
                        },
                        error:function (res) {
                            layui.loading.loadRemove(1);
                            return layui.popup.failure('请求失败');
                        }
                    });
                })

                layui.$('.edit_original_content_ai_command').click(function () {
                    layui.$('textarea[name="original_content_ai_command_overseas"').val(layui.$('textarea[name="original_content_ai_command_config"').val());
                });
            });

            function comput_original_nums() {
                let content = layui.$('textarea[name="original_content"]').val();
                layui.$('textarea[name="original_content"]').val(content);

                $('.original_char_nums').text(content.length);
            }

            function comput_analyze_nums() {
                let content = layui.$('textarea[name="content"]').val();
                layui.$('textarea[name="content"]').val(content);
                $('.analyze_char_nums').text(content.length);
            }
        </script>

    </body>

</html>
